package com.example.administrator.swredeem.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.swredeem.R;
import com.example.administrator.swredeem.appBase.Config;
import com.example.administrator.swredeem.beanClass.GongdanListBean;
import com.example.administrator.swredeem.beanClass.UserListBean;
import com.example.administrator.swredeem.net.HttpXutils;
import com.example.administrator.swredeem.net.MyCallback;
import com.example.administrator.swredeem.utils.LiveDataBus;
import com.example.administrator.swredeem.view.adapter.UserListAdapter;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GongdanDetailsActivity extends AppCompatActivity {
    GongdanListBean.InfoBean infoBean;
    @BindView(R.id.gdbh)
    TextView gdbh;
    @BindView(R.id.gdlx)
    TextView gdlx;
    @BindView(R.id.khnc)
    TextView khnc;
    @BindView(R.id.khbh)
    TextView khbh;
    @BindView(R.id.lxfs)
    TextView lxfs;
    @BindView(R.id.khsfzh)
    TextView khsfzh;
    @BindView(R.id.khdz)
    TextView khdz;
    @BindView(R.id.isdh)
    TextView isdh;
    @BindView(R.id.dhtype)
    TextView dhtype;

    private UserListAdapter userListAdapter;
    private UserListBean userListBean;
    private Gson gson;
    private String elementsBeanjson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gongdan_details);
        ButterKnife.bind(this);
        init();

    }

    private void init() {
        gson = new Gson();
        Intent intent = getIntent();
        Log.d("TAG", "init: " + intent.getStringExtra("GdDetailData"));
        infoBean = gson.fromJson(intent.getStringExtra("GdDetailData"), GongdanListBean.InfoBean.class);
        updata();
    }

    private void updata() {
        gdbh.setText("工单编号: " + infoBean.getPkid());
        gdlx.setText("工单类型: " + infoBean.getTypevalue());
        khbh.setText("客户编号: " + infoBean.getUserno());
        khnc.setText("客户名称: " + infoBean.getUsername());
        lxfs.setText("联系方式: " + infoBean.getMobile());
        khsfzh.setText("客户身份证号: " + infoBean.getCertnum());
        khdz.setText("客户地址: " + infoBean.getAddr());
        isdh.setText("是否点火: " + infoBean.getIsignition());
        dhtype.setText("点火状态: " + infoBean.getIgnistatus());
        getdata();
    }

    public void goDianhuo(View view) {
        if (infoBean.getIgnistatus().equals("未录入")) {
            Intent intent = new Intent(GongdanDetailsActivity.this, DianhuoGbActivity.class);
            intent.putExtra("workid", infoBean.getPkid());
            startActivity(intent);
        }
    }

    public void goKaihu(View view) {

        Intent intent = new Intent(GongdanDetailsActivity.this, UserDataActivity.class);
        intent.putExtra("title", "开户");
        intent.putExtra("elementsBean", elementsBeanjson);
        Log.d("TAG", "goKaihu: "+elementsBeanjson);
        startActivity(intent);

    }

    public void getdata() {

        Map<String, String> map = new HashMap<>();
        map.put("spotno", "1");
        map.put("operno", "1");
        map.put("orgcode", "1");
        map.put("dataorgid", "1");
        map.put("type", "1");
        map.put("qureyid", infoBean.getUserno());

        HttpXutils.getData(Config.ICUser, map, new MyCallback() {
            @Override
            public void onSuccess(String result) {
                Log.d("TAG", "onSuccess: " + result);
                userListBean = gson.fromJson(result, UserListBean.class);
                if (userListBean.getInfo() != null && userListBean.getInfo().size() > 0) {
                    elementsBeanjson =  gson.toJson(userListBean.getInfo().get(0));
                } else {
                    Toast.makeText(GongdanDetailsActivity.this, "查无此户", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("TAG", "onError: " + ex.getMessage() + isOnCallback);

                Toast.makeText(GongdanDetailsActivity.this, "网络错误，请联系管理员", Toast.LENGTH_SHORT).show();
            }
        });
    }


}
